Skip to content

Guard shared_ so a logger may report while close() runs - #15

Merged
dmccoystephenson merged 1 commit into
mainfrom
feature/guard-shared-against-close
Oct 22, 2026
Merged

dmccoystephenson merged 1 commit into
mainfrom
feature/guard-shared-against-close

Conversation

@dmccoystephenson

Copy link
Copy Markdown
Member

Summary

  • TraceClient::shared_ is now guarded by a mutex of its own (sharedMutex_). close() swaps it out under that lock; report() (via enqueue) and isEnabled() work from a copy taken under it (current()). Previously close() swapped shared_ with no lock while a Logger on the sending thread could be reading it through report() — the ThreadSanitizer data race described in report() from inside the Logger races with close() on TraceClient::shared_ #14.
  • The first direction proposed in report() from inside the Logger races with close() on TraceClient::shared_ #14 was taken, but with a plain std::mutex rather than std::atomic_load on the shared_ptr: the atomic free functions are deprecated in C++20 and would trip -Werror on the c++20 legs.
  • Behaviour is otherwise unchanged: a report made by the logger after close() has swapped the state out is dropped, as before. The lock is held only for the swap/copy, never while close() waits, so a logger calling report() cannot deadlock against close(). Every new lock acquisition is inside a try, so report, close, isEnabled and the destructor still never throw.
  • New test aLoggerMayReportWhileCloseIsRunning (registered in tests[]): the logger signals the main thread, which calls close() while the logger sleeps and then calls report(), so nothing orders the swap against the read. This is the unsynchronised schedule from report() from inside the Logger races with close() on TraceClient::shared_ #14; the comment in aLoggerMayItselfReport now points at it.

Closes #14

Deferred

Test plan

  • make test (g++, C++11, -Werror): 54 tests, 0 failed checks (53 before)
  • make test STD=c++17 and STD=c++2a (local g++ lacks -std=c++20): 54 tests, 0 failed checks
  • CI linux-sanitizers (thread) green — the local toolchain has no TSan runtime, so this is the anchor for the race; a temporary CI revert of the header is used to show the new test fails under TSan without the fix
  • Remaining CI matrix (linux, linux-libcurl, address/undefined, macos, windows) green

This PR description was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).

🤖 Generated with Claude Code


drafted by Claude on behalf of Daniel Stephenson

close() swapped shared_ out with no lock while the logger, on the
sending thread, could be reading it through report(). A mutex now
guards shared_ alone: close() swaps under it and report() and
isEnabled() work from a copy taken under it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dmccoystephenson

Copy link
Copy Markdown
Member Author

Self-review rubric (anchored on CI run 39781398795 at head 72edf56, all 11 jobs green):

  • Scope: PASS. Only trace_client.hpp (the lock on shared_) and test/test_trace_client.cpp (the new test, plus a one-line comment update in aLoggerMayItselfReport) changed. Net change: +61/−11.
  • Tests-new: PASS. No public API was added. The private current() is exercised through report() and isEnabled() by the new test and by every existing test.
  • Tests-fix: PASS, shown with CI-based temporary revert (fallback ladder, rung 1). The local g++ has no TSan runtime (cannot find libtsan_preinit.o). Commit ca8cd84 (TEMP: revert 72edf56 …) restored the old header and kept the new test. linux-sanitizers (thread) then failed with WARNING: ThreadSanitizer: data race, the write in TraceClient::close at trace_client.hpp:1040 and the read in enqueue at :1139, both in aLoggerMayReportWhileCloseIsRunning (run 39781149320). The branch was then reset to 72edf56 and force-pushed, and the run went green again (39781398795). In that red run, linux-sanitizers (address,undefined) showed as failed only because fail-fast cancelled it: all of its steps succeeded and it printed 54 tests, 0 failed checks.
  • Sibling structure / renames: PASS. No new files, nothing renamed.
  • Docs: PASS. The README does not describe report() from a logger during close(), and every promise it makes (returns immediately, never throws, close() drains, the logger is silent after close) still holds. The logMutex comment in Shared is now true even while close() runs.
  • Issue resolution: PASS. report() from inside the Logger races with close() on TraceClient::shared_ #14's named surface (close()'s swap, enqueue's read) is changed, and the test it suggests (a logger reporting while close() runs, with no handshake) is added.
  • CI: PASS. All 11 jobs are green on 72edf56.
  • Registered: PASS. aLoggerMayReportWhileCloseIsRunning appears twice in the file. The local count went from 53 to 54 tests, 0 failed checks.
  • C++11: PASS. Locally, make test (c++11, -Werror) is clean, and so are c++17 and c++2a. All six CI linux legs are green. std::atomic_load on shared_ptr was deliberately avoided because it is deprecated in C++20.
  • No new dependency: PASS. The change uses only std::mutex and std::lock_guard, which the header already uses. No #include and no link flag was added.
  • Never throws: PASS. Each new lock_guard sits inside a try. current() returns an empty pointer if the lock fails, and close() returns. Copying a shared_ptr is noexcept.
  • Curl argv fixed: PASS. Untouched.
  • Version triple: PASS. Untouched.
  • Sanitizers: PASS. Both linux-sanitizers jobs are green on 72edf56.

Judgment notes:

  • trace_client.hpp start(): shared_ = shared is still written without sharedMutex_. This is safe because it runs inside the constructor before any report() is possible, and nothing is queued for the logger to react to. It was left alone to keep the diff narrow.
  • test/test_trace_client.cpp aLoggerMayReportWhileCloseIsRunning: the logger sleeps 200 ms before it reports, so close() very likely swaps first and "logged" is dropped. The test therefore asserts only that "startup" arrived, that the logger's report() returned, and that the client is disabled. TSan is what detects the race.
  • Out of diff: init()'s catch resets shared_ while thread_ may already be joinable, so a throw after start() leads to std::terminate in the destructor. Filed as A throw in init() after start() leaves thread_ joinable, so the destructor calls std::terminate #16 and not addressed here.

Do-not-auto-merge check: the diff touches no curl argv, quoting, spawn, proto, version or wire-format code, and no Makefile, CMakeLists.txt or workflow file. No file loses more than 50 lines.

This review comment was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

@dmccoystephenson
dmccoystephenson merged commit dbe0c41 into main Oct 22, 2026
22 checks passed
@dmccoystephenson
dmccoystephenson deleted the feature/guard-shared-against-close branch October 22, 2026 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

report() from inside the Logger races with close() on TraceClient::shared_

1 participant